<template>
	<view>
		<view class="loginbox">
			<!-- <u--input placeholder="请输入手机号" border="bottom" clearable placeholder-class="zzz" v-model="phone"></u--input> -->
			<input placeholder="请输入手机号" border="bottom" clearable placeholder-class="zzz" v-model="phone" />
			<!-- <u--input class="u-m-tb-40" placeholder="请输入验证码" border="bottom" clearable placeholder-class="zzz" v-model="code"> -->
			<view class="inputbox">
				<input class="u-m-tb-40" placeholder="请输入验证码" border="bottom" clearable placeholder-class="zzz"
					v-model="code" />

				<template>
					<!-- <u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></u-code>
					<text class="codestyle" @tap="getCode">{{tips}}</text> -->
					<view class="code-img-wrapper" @click="updateImageCode">
						<canvas style="width:220rpx;height:86rpx;" canvas-id="canvas"></canvas>
					</view>
				</template>
			</view>

			<view class="readstyle">
				登录代表您已阅读并同意
				<text class="special">《用户服务协议》</text>和
				<text class="special">《隐私政策》</text>
			</view>
			<view class="denglu u-font-bold" @click="tohome">
				登录
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		Mcaptcha
	} from '@/utils/mcaptcha'
	export default {
		data() {
			return {
				phone: '',
				code: '',
				tips: '',
				value: '',
				mcaptcha: "",
			};
		},
		mounted() {
			this.mcaptcha = new Mcaptcha({
				el: 'canvas',
				width: 80,
				height: 35,
				createCodeImg: ""
			});
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			// 刷新验证码
			updateImageCode() {
				this.mcaptcha.refresh()
			},
			tohome() {
				// if (!this.phone) {
				// 	this.$toast('请输入手机号~')
				// 	return
				// }
				// let validate = this.mcaptcha.validate(this.code)
				// if (!validate) {
				// 	return this.$toast('图形验证码错误!')
				// }
				// this.$api.post('/user/mobilelogin', {
				// 	mobile: this.phone,
				// 	captcha: this.code
				// }).then(res => {
				// 	if (res.code == 1) {
				// 		this.$toast(res.msg)
				// 		uni.setStorageSync('token', res.data.userinfo.token)
				// 		uni.setStorageSync('userinfo', res.data.userinfo)
				// 		setTimeout(() => {
				// 			uni.redirectTo({
				// 				url: '/pages/my/my'
				// 			})
				// 		}, 1000)
				// 	}
				// })
				uni.switchTab({
					url: '/pages/index/index'
				})

			},
			getCode() {
				if (!this.phone) {
					this.$toast('请输入手机号~')
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						this.$api.post('/sms/send', {
							mobile: this.phone
						}).then(res => {
							if (res.code == 1) {
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							}

						})

					}, 1000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FDFBF6;
		overflow: hidden;
	}

	.inputbox {
		display: flex;
		align-items: center;
	}

	.loginbox {
		width: 636rpx;
		margin: 400rpx auto 0;

		input {
			border-bottom: 1rpx solid #E6E6E6;
			padding: 20rpx;
		}

		.zzz {
			color: #B3B3B3;
			font-weight: bold;
			font-size: 34rpx;
		}

		.codestyle {
			color: #D05E30;
		}

		.readstyle {
			font-size: 24rpx;
			text-align: center;

			.special {
				color: #576B95;
			}
		}

		.denglu {
			margin: 100rpx auto 0;
			line-height: 88rpx;
			text-align: center;
			color: #fff;
			width: 440rpx;
			height: 88rpx;
			background: #D05E30;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
		}
	}
</style>